﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Infrastructure;
using Microsoft.Extensions.Options;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;

namespace OpenAuth.App
{
    public enum QrType
    {
        /// <summary>
        /// 门诊
        /// </summary>
        MZ,
        /// <summary>
        /// 住院
        /// </summary>
        ZY
    }
    public class QRPayApp : ApiBaseApp<BankParam>
    {
        private IOptions<BankParam> bankParam;
        private PayOrderApp payApp;
        public QRPayApp(IApiRepository<BankParam> repository, IOptions<BankParam> _bankParam, PayOrderApp _payApp) : base(repository)
        {
            bankParam = _bankParam;
            payApp = _payApp;
        }
        /// <summary>
        /// 获取付款二维码
        /// </summary>
        /// <param name="patid">患者id</param>
        /// <param name="name">患者姓名</param>
        /// <param name="amount">充值金额</param>
        /// <returns></returns>
        public PaySecond GetQR(string patid, string name, string phone, string card, float amount, QrType qrType)
        {
            //订单编号(必须为数字)
            string orderId = UniqueData.Gener("");
            //准备需要md5的字符串（不能出现汉字）
            string _mac = "MERCHANTID=" + bankParam.Value.MERCHANTID + "&POSID=" + bankParam.Value.POSID + "&BRANCHID=" + bankParam.Value.BRANCHID +
                        "&ORDERID=" + orderId + "&PAYMENT=" + amount.ToString() + "&CURCODE=01&TXCODE=530550&REMARK1=" + $"{orderId}" +
                        "&REMARK2=" + $"{patid}|{phone ?? ""}|{card ?? ""}" + "&RETURNTYPE=" + bankParam.Value.RETURNTYPE + "&TIMEOUT=" + "&PUB=" + bankParam.Value.PUB;
            //md5加密
            string mac = Md5.Get32MD5(_mac).ToLower();
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("CCB_IBSVersion", bankParam.Value.CCB_IBSVersion);
            dic.Add("MERCHANTID", bankParam.Value.MERCHANTID);
            dic.Add("POSID", bankParam.Value.POSID);
            dic.Add("BRANCHID", bankParam.Value.BRANCHID);
            dic.Add("ORDERID", orderId);
            dic.Add("PAYMENT", amount.ToString());
            dic.Add("CURCODE", bankParam.Value.CURCODE);
            dic.Add("REMARK1", $"{orderId}|{qrType}");//写入订单号，建行回调时，根据订单号充值
            dic.Add("REMARK2", $"{patid}|{phone ?? ""}|{card ?? ""}");
            dic.Add("TXCODE", bankParam.Value.TXCODE);
            dic.Add("RETURNTYPE", bankParam.Value.RETURNTYPE);
            dic.Add("TIMEOUT", "");//DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")
            dic.Add("MAC", mac);
            HttpHelper webClient = new HttpHelper("http://192.168.0.33:8080/");
            string firstResult = webClient.Get(dic, bankParam.Value.URL);
            //string PAYURL = HttpUtility.UrlDecode(Json.ToObject<PayFirst>(firstResult).PAYURL);
            string PAYURL = Json.ToObject<PayFirst>(firstResult).PAYURL;
            string secondResult = webClient.Get(null, PAYURL);
            string QRURL = HttpUtility.UrlDecode(Json.ToObject<PaySecond>(secondResult).QRURL);
            //payApp.Add(new Request.AddOrUpdatePayOrderReq { OrderNumber = orderId, bxh = card, hzxm = name, zfje = amount, zfzt = 0, time = DateTime.Now, zfPhone = phone, patid = patid });
            return new PaySecond { QRURL = QRURL, ORDERID = orderId };
        }

    }
}
